import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';

//main(List<String> args) {
//  runApp(Basewhidget());
//}

class Basewhidget extends StatelessWidget {
  // statelessWidget 是一个无状态的组件，用于自定义组件

  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      // materialAPP 是作为根组件使用，定义该页面展示的总体布局
        home: Home()
    );
  }

}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("滑动控制"),
      ),
      body: MyHomePage(),
//      回到最上页
      floatingActionButton: !true
          ? null
          : FloatingActionButton(
        onPressed: () {
          _scrollController.jumpTo(10);
        },
        child: Icon(Icons.arrow_upward),
      ),

    );
  }
}


class MyHomePage extends StatefulWidget {
  final String title;

  const MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Scontroller1 createState() => Scontroller1();
}



class Scontroller1 extends State<MyHomePage> {
//   获取控制器，用于监听滑动事件
  ScrollController _scrollController = new ScrollController(initialScrollOffset: 0);
  bool isShow = false;
  String _progress = "0%";
  int Nums = 4;
  TextStyle isshow = TextStyle(color: Colors.black12);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      int offset = _scrollController.position.viewportDimension.toInt();
      print("滑动距离$offset");

      // 如果滑动到底部
      if (_scrollController.offset ==
          _scrollController.position.maxScrollExtent) {
        setState(() {
          print("滑到底部了");
          setState(() {
            this.Nums += 3;   // 动态添加展示数据
          });

          isShow = true;
        });
      } else {
        setState(() {
          isShow = false;
        });
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Container(
//        padding: EdgeInsets.all(20),
      child: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          Container(
            child: Text("松开即可刷新", style: this.isshow,),

          ),
          Scrollbar(
            child: NotificationListener(
              // ignore: missing_return
                onNotification: (ScrollNotification notification) {
                  //滑动进度/最大可滑动进度
                  double progress = notification.metrics.pixels /
                      notification.metrics.maxScrollExtent;
                  setState(() {
                    _progress = "${((progress * 100).toInt())}%";
                  });
                },
                child: ListView.separated(
                    physics: BouncingScrollPhysics(),
                    controller: _scrollController,
                    itemBuilder: (BuildContext buildContext, int index) {
                      print("index是 $index, Nmus 是$Nums");
                      return Container(
                        margin: EdgeInsets.fromLTRB(10, 5, 10, 5),
                        child: Row(
                          children: <Widget>[

                            Expanded(
                              child: InkWell(
                                onLongPress: (){     // 长按事件
                                    print("被长按了");
                                      showDialog<Null>(    // 弹窗
                                        context: context,
                                        barrierDismissible: false,
                                        builder: (BuildContext context) {
                                          return new AlertDialog(
//                                            title: new Text('标题'),
                                            content: new SingleChildScrollView(
                                              child: Center(
                                                child: Text("下载到本地"),
                                              )
                                            ),
                                            actions: <Widget>[
                                              new FlatButton(
                                                child: new Text('确定'),
                                                onPressed: () {
                                                  Navigator.of(context).pop();
                                                },
                                              ),

                                              new FlatButton(
                                                child: new Text('取消'),
                                                onPressed: () {
                                                  Navigator.of(context).pop();
                                                },
                                              ),
                                            ],
                                          );
                                        },
                                      ).then((val) {
                                        print(val);
                                      });

                      },
                                  child: Image.network(
                                    // ignore: unnecessary_brace_in_string_interps
                                    "https://api.iehu.cn/Image.php?a=${index}",
                                    fit: BoxFit.cover,
                                  )
                        )
                                )
                          ],
                        ),
                      );

                    },
                    separatorBuilder: (BuildContext buildContext, int index) {
                      return Divider(
                        color: Colors.grey,
                      );
                    },
                    itemCount: this.Nums)),  // 这里是滑动数量
          ),
//            CircleAvatar(
//              //显示进度百分比
//              radius: 30.0,
//              child: Text(_progress),
//              backgroundColor: Colors.black54,
//            ),
        ],
      ),
    );

  }
}

